//
//  DetailViewController.m
//  GeoPhoto
//
//  Created by lis on 1/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DetailViewController.h"
#import "MasterViewController.h"


@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation DetailViewController

@synthesize detailItem = _detailItem;
@synthesize detailDescriptionLabel = _detailDescriptionLabel;
@synthesize masterPopoverController = _masterPopoverController;
@synthesize imageView;
@synthesize scrollView;
@synthesize imageView2;
//@synthesize imageView3;
//@synthesize imageView4;
//@synthesize imageView5;
@synthesize button; 
@synthesize refresh;
@synthesize first;
@synthesize second;
@synthesize third;
@synthesize fourth;
@synthesize fifth;
@synthesize images;
#pragma mark - Managing the detail item


- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        
        // Update the view.
        [self configureView];
    }
    
    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.
    
    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    if (self.first) return imageView;
    else if (self.second) return imageView2;
    else if (self.third) return imageView;
    else if (self.fourth) return imageView2;
    else return imageView;
    
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"blank.jpg"]];
    imageView2 = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"blank.jpg"]];
    //imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"blank.jpg"]];
    //imageView2 = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"blank.jpg"]];
    //imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"blank.jpg"]];
    
    
    //NSLog(@"frame1 dimensions %f%f", imageView.frame.size.width, imageView.frame.size.height);
    //NSLog(@"frame2 dimensions %f%f", imageView2.frame.size.width, imageView2.frame.size.height);
    
    self.first = TRUE;
    self.second = FALSE;
    self.third = FALSE;
    self.fourth = FALSE;
    self.fifth = FALSE;
    scrollView.frame = self.view.frame;
    scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    scrollView.maximumZoomScale = 100.0;
    scrollView.minimumZoomScale =0.05;
    scrollView.clipsToBounds = YES;
    CGRect tempframe = imageView.frame;
    tempframe.size.height = scrollView.frame.size.height;
    imageView.frame  = tempframe;
    [scrollView addSubview:imageView];
	// Do any additional setup after loading the view, typically from a nib.
    [self configureView];
    
}


-(IBAction)refresh:(id)sender {
    NSInteger len = [self.images count];
    
    if (len == 0) {
        [imageView2 removeFromSuperview];
        [imageView setImage:[UIImage imageNamed:@"blank.jpg"]];
        
    }
    else if (len ==1) {
        self.first = TRUE;
        [imageView2 removeFromSuperview];
        [scrollView addSubview:imageView];
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        
    }
    else if (len <=5) {
        //[scrollView addSubview:imageView2];
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Too many images selected" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [alert show];
    }
    
    /*
    if (len == 0) {
        [imageView setImage:[UIImage imageNamed:@"blank.jpg"]];
        [imageView2 removeFromSuperview];
        [imageView3 removeFromSuperview];
        [imageView4 removeFromSuperview];
        [imageView5 removeFromSuperview];
    }
    else if (len == 1){
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 removeFromSuperview];
        [imageView3 removeFromSuperview];
        [imageView4 removeFromSuperview];
        [imageView5 removeFromSuperview];
    }
    else if (len == 2){
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
        [imageView3 removeFromSuperview];
        [imageView4 removeFromSuperview];
        [imageView5 removeFromSuperview];
    }
    else if (len == 3) {
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
        [imageView3 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
        [imageView4 removeFromSuperview];
        [imageView5 removeFromSuperview];
    }
    else if (len == 4) {
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
        [imageView3 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
        [imageView4 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
        [imageView5 removeFromSuperview];
    }
    else if (len == 5) {
        [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
        [imageView3 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
        [imageView4 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
        [imageView5 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:4]]];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Too many images selected" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [alert show];
    }
     */
}

-(IBAction)toggle:(id)sender{

    NSInteger theLength = [self.images count];
    
    if (theLength == 0) {
    }
    else if (theLength == 1) {
        [scrollView addSubview:imageView];
    }
    
    else if (theLength == 2) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            //NSLog(@"%f%f", imageView.frame.size.width, imageView.frame.size.height);
            //NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
            first = FALSE;
            second = TRUE;
        }else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
        }
    }
    
    else if (theLength == 3) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
        }
        
    }
    
    else if (theLength == 4) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;
            
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;
            
        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;
            
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            
        }
        
    }
    
    else if (theLength == 5) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;
            fifth = FALSE;
            
        }
        else if (fourth) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:4]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = TRUE;
            
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        
    }
    
    /*
    if (theLength == 0) {
        }
    else if (theLength == 1) {
        [scrollView addSubview:imageView];
    }
    else if (theLength == 2) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            //NSLog(@"%f%f", imageView.frame.size.width, imageView.frame.size.height);
            //NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
            first = FALSE;
            second = TRUE;
        }else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
        }
    }
    
    else if (theLength == 3) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView3.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView3.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView3.frame.size.width, imageView3.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView3];
            first = FALSE;
            second = FALSE;
            third = TRUE;
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView3.frame.size;
            [imageView3 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
        }
        
    }
    
    else if (theLength == 4) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;

        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView3.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView3.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView3.frame.size.width, imageView3.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView3];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;

        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView3.frame.size;
            [imageView3 removeFromSuperview];
            CGRect frame = imageView4.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView4.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView4.frame.size.width, imageView4.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView4];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;

        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView4.frame.size;
            [imageView4 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;

        }
        
    }
    
    else if (theLength == 5) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView3.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView3.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView3.frame.size.width, imageView3.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView3];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView3.frame.size;
            [imageView3 removeFromSuperview];
            CGRect frame = imageView4.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView4.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView4.frame.size.width, imageView4.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView4];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;
            fifth = FALSE;
            
        }
        else if (fourth) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView4.frame.size;
            [imageView4 removeFromSuperview];
            CGRect frame = imageView5.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView5.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView5.frame.size.width, imageView5.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView5];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = TRUE;
            
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView5.frame.size;
            [imageView5 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        
    }
    
    else NSLog(@"Error: Too many pictures");
     */
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

@end
